package com.shine.app.ui;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shine.app.AppManager;
import com.shine.app.BaseActivity;
import com.shine.app.R;
import com.shine.app.bean.MyFavoriteBean;
import com.shine.app.bean.responses.ValidateResponse;
import com.shine.app.commons.UIHelper;
import com.shine.app.inject.InjectView;
import com.shine.app.inject.Injector;

public class ViewMyInfo_FavoriteDeleteDialogActivity extends BaseActivity
		implements OnClickListener {

	@InjectView(R.id.submit)
	private TextView submiTextView;
	@InjectView(R.id.cancel)
	private TextView cancelTextView;
	@InjectView(R.id.message)
	private TextView messageTextView;
	@InjectView(R.id.layout)
	private LinearLayout layout;
	@InjectView(R.id.progressBar)
	private ProgressBar progressBar;
	private MyFavoriteBean myFavoriteBean;
	private FavoriteRemoveAsyncTask favoriteRemoveAsyncTask;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.dialog_cancelteetimes);
		Injector.get(this).inject();

		init();
	}

	private void init() {
		progressBar.setVisibility(View.GONE);
		myFavoriteBean = (MyFavoriteBean) getIntent().getSerializableExtra(
				"myFavoriteBean");
		cancelTextView.setText(R.string.cancel);
		submiTextView.setText(R.string.delete);
		messageTextView.setText(R.string.favorite_deletedialog);
		submiTextView.setOnClickListener(this);
		cancelTextView.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel:
			AppManager.getAppManager().finishActivity();
			break;
		case R.id.submit:
			layout.setVisibility(View.GONE);
			String golferId = appContext.getLoginInfo().getUser().getGolferId();
			favoriteRemoveAsyncTask = new FavoriteRemoveAsyncTask();
			favoriteRemoveAsyncTask.execute(myFavoriteBean.getId(), golferId);
			break;

		default:
			break;
		}
	}

	public class FavoriteRemoveAsyncTask extends
			AsyncTask<Object, Integer, ValidateResponse> {
		private int courseId;
		private String golferId;

		@Override
		protected ValidateResponse doInBackground(Object... params) {
			ValidateResponse response = null;
			courseId = (Integer) params[0];
			golferId = (String) params[1];
			response = appContext.removeFavoriteCourse(golferId, courseId);
			return response;
		}

		@Override
		protected void onPostExecute(ValidateResponse result) {
			super.onPostExecute(result);
			if (result != null && result.isData()) {
				UIHelper.toastDialog(
						ViewMyInfo_FavoriteDeleteDialogActivity.this,
						R.string.msg_favorite_remove_success, R.string.msg_yes);
			} else {
				UIHelper.toastDialog(
						ViewMyInfo_FavoriteDeleteDialogActivity.this,
						R.string.msg_favorite_remove_failed, R.string.msg_yes);
			}
			AppManager.getAppManager().finishActivity();
			progressBar.setVisibility(View.GONE);
			overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onDestroy() {
		if (favoriteRemoveAsyncTask != null
				&& favoriteRemoveAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			favoriteRemoveAsyncTask.cancel(true);
		}
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AppManager.getAppManager().finishActivity();
		}
		return true;
	}
}
